package com.heneng.smokersdktest;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.heneng.smokersdk.SmokerSdk;
import com.heneng.smokersdk.fragment.CameraFragment;
import com.heneng.smokersdk.listener.OnCollectStateListener;
import com.heneng.smokersdk.listener.OnConnectSmokerListener;


public class MainActivity extends AppCompatActivity {

    private TextView mTvConnectSmokerState;
    private TextView mTvCollectState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //初始化，必须先调用
        SmokerSdk.init(getApplicationContext());
        //设备连接状态监听
        SmokerSdk.getInstance().setOnConnectSmokerListener(new OnConnectSmokerListener() {
            @Override
            public void connectSuccess() {
                Log.d("simonfong", "连接成功");
                //连接成功后再加载CameraFragment，加载成功后便可正常使用检测
                CameraFragment cameraFragment = new CameraFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.fl_fragment, cameraFragment).commit();
                mTvConnectSmokerState.setText("设备连接状态：" + "连接成功");
            }

            @Override
            public void connecting() {
                Log.d("simonfong", "正在连接");
                mTvConnectSmokerState.setText("设备连接状态：" + "正在连接");
            }

            @Override
            public void connectFail(int errorCode, String errorMsg) {
                Log.d("simonfong", "连接失败：" + errorMsg);
                mTvConnectSmokerState.setText("设备连接状态：" + "连接失败：" + errorMsg);
            }
        });
        //检测过程状态监听
        SmokerSdk.getInstance().setOnCollectStateListener(new OnCollectStateListener() {
            @Override
            public void collectState(int stateCode, String stateMsg, String maxGrade, String curGrade) {
                //
                Log.d("simonfong", "检测状态：" + stateMsg + "---" + maxGrade + "---" + curGrade);
                mTvCollectState.setText("检测过程状态：" + stateMsg + "---最大值：" + maxGrade + "---当前值：" + curGrade);
            }

            @Override
            public void error(int errorCode, String errorString) {
                Log.d("simonfong", "设备报错：" + errorString);
                mTvCollectState.setText("检测过程状态：" + "设备报错：" + errorString);
            }
        });
    }

    private void initView() {
        mTvConnectSmokerState = findViewById(R.id.tv_connect_smoker_state);
        mTvCollectState = findViewById(R.id.tv_collect_state);

        findViewById(R.id.btn_connect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //连接前必须保证wifi关闭，热点打开，第一次连接需要的时间大概2,3分钟，如果连接不上，重启设备和app
                SmokerSdk.getInstance().connectSmoker();
            }
        });
        findViewById(R.id.btn_start_collect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //开始检测（包括校准和检测操作，状态在OnCollectStateListener返回，在校准完毕后方可汽车喷气）
                SmokerSdk.getInstance().startTest();
            }
        });
        findViewById(R.id.btn_stop_collect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //停止检测（状态在OnCollectStateListener返回）
                SmokerSdk.getInstance().stopTest();
            }
        });
    }
}
